package bar.dark.lee.alarm;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import bar.dark.lee.alarm.common.BDAdView;
import bar.dark.lee.alarm.common.DBSQLite;
import bar.dark.lee.alarm.common.SuperInterface;
import bar.dark.lee.alarm.query.DBQuery;

public abstract class SuperActivity extends Activity implements SuperInterface{

	public Context superContext;
	public DBSQLite sql;
	public DBQuery query;

	public int purchase = BDContents.PURCHASE;
	
	private static Typeface typeface;
	
	private View titleLayout;
	private TextView titleView;
	private TextView leftButton;
	private TextView rightButton;
	private ViewGroup bodyLayout;
	
	public ViewGroup superAdView;
	
	private BDAdView adview = null;
	
	public boolean isAdFlag = true;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.super_activity);
		
		superContext = this;
		sql = DBSQLite.getInstance(superContext);
		query = new DBQuery(this);
		
		titleLayout = findViewById(R.id.titleLayout);
		titleView = (TextView) findViewById(R.id.superTitleView);
		bodyLayout = (ViewGroup) findViewById(R.id.bodyLayout);
		leftButton = (TextView) findViewById(R.id.superLeftButton);
		rightButton = (TextView) findViewById(R.id.superRightButton);
		
		superAdView = (ViewGroup) findViewById(R.id.superAdview);
		
		leftButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				onBackPressed();
			}
		});
		
		rightButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				tipButton();
			}
		});
		
		if (purchase == BDContents.PURCHASE_FREE)
		{
			isAdFlag = true;
		}
		else if (purchase == BDContents.PURCHASE_PAY)
		{
			isAdFlag = false;
		}
			
		if (isAdFlag)
		{
			startAd();
		}
	}
	
	protected void startAd()
	{
		adview = new BDAdView(this, (LinearLayout)superAdView);
		//adview.startAdam();
//		adview.startTad();
//		adview.startBDAd();
	}
	
	@Override
	protected void onResume()
	{
		super.onResume();
		
		if (adview != null)
		{
			adview.BDAdViewResume();
		}
	}
	
	@Override
	protected void onPause()
	{
		super.onPause();
		if (adview != null)
		{
			adview.BDAdViewPause();
		}
	}
	
	@Override
	protected void onDestroy()
	{
		super.onDestroy();
		
		if (sql != null)
		{
			sql.closeDatabese();
		}
		
		if (adview != null)
		{
			adview.BDAdViewDestroy();
		}
	}
	
	@Override
	protected void onNewIntent(Intent intent)
	{
		super.onNewIntent(intent);
	}
	
	@Override
	public void setContentView(int layoutResID)
	{
		LayoutInflater inflater = getLayoutInflater();
		View view = inflater.inflate(layoutResID, null);
		this.setContentView(view);
	}
	
	@Override
	public void setContentView(View view) {
		this.bodyLayout.removeAllViews();
		
		if(view==null)
		{
			return;
		}
		
		ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
		
		if(layoutParams == null)
		{
			layoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		}
		else
		{
			layoutParams.width = LayoutParams.FILL_PARENT;
			layoutParams.height = LayoutParams.FILL_PARENT;
		}
		
		view.setLayoutParams(layoutParams);
		
		this.bodyLayout.addView(view);
		
		if (typeface == null)
			typeface = Typeface.createFromAsset(getAssets(), BDContents.FONT_FILE_NAME);

        ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
        setGlobalFont(root);
	}
	
	private void setGlobalFont(ViewGroup root) 
	{
	    for (int i = 0; i < root.getChildCount(); i++) 
	    {
	        View child = root.getChildAt(i);
	        
	        if (child instanceof TextView)
	        {
	        	((TextView)child).setTypeface(typeface);
	        }
	        else if (child instanceof EditText)
			{
	        	((EditText)child).setTypeface(typeface);
			}
	        else if (child instanceof Button)
			{
	        	((Button)child).setTypeface(typeface);
			}
	        else if (child instanceof ViewGroup)
	        {
	        	setGlobalFont((ViewGroup)child);
	        }
	    }
	}
	
	@Override
	public void setTitle(CharSequence title)
	{
		titleView.setText(title);
	}

	@Override
	public void setTitle(int titleId)
	{
		titleView.setBackgroundResource(titleId);
	}
	
	/**
	 * getString(int stringId) 편하게 쓰기위해 만들어놓음.
	 * @param stringId
	 * @return
	 */
	public String gets(int stringId)
	{
		return getString(stringId);
	}
	
	/**
	 * 오른쪽 팁 버튼을 안보이게 한다.
	 */
	public void setGoneTipButton()
	{
		rightButton.setVisibility(View.INVISIBLE);
	}
	
	/**
	 * 타이틀을 안보이게 한다.
	 */
	public void setGoneTitle()
	{
		titleLayout.setVisibility(View.GONE);
	}
	
	/**
	 * 도움말 버튼의 문구를 변경한다.
	 * @param text
	 */
	public void setTextTip(String text)
	{
		rightButton.setText(text);
	}
	
	/**
	 * 도움말 버튼입니다.
	 */
	protected abstract void tipButton();
}
